Paul O'Leary
Comp 4433 - Data Visualizations
Project - Various plots of Alcohol Consumption and Wine Grape Production
My wife and I recently finished our basement, adding a well-stocked bar and a wine room. We both enjoy a nice glass of wine with dinner, and I like a glass of good whiskey or bourbon after a long week. I had previously looked at various Wine review datasets on Kaggle and other repositories, but after a number of searches, I discovered an extensive collection of data from the University of Adelaide in Australia.
The data is available here: https://economics.adelaide.edu.au/wine-economics/databases#database-of-regional-national-and-global-winegrape-bearing-areas-by-variety-1960-to-2016 and includes data from Australia and the world on various wine and beverage consumption topics.
Notes:
# Basic beginning libraries
import pandas as pd
import numpy as np
Initially, I looked at Wine, Beer and Spirits Consumption per capita. There are gaps in the data - most notably during Prohibition in the US and during World War 2, and there just is not data for many countries. However, I think the graphs still show some interesting trends.
What follows is a fair amount of cleanup to get the data into a usable form for Plotly Choropleths.
# Import Wine consumption per capita
wine_per_cap = pd.read_csv('Wine_per_cap.csv', skiprows=1)
wine_per_cap.head()
| Year | France | Italy | Portugal | Spain | Austria | Bel-Lux | Denmark | Finland | Germany | ... | Japan | Korea | Malaysia | Philippines | Singapore | Taiwan | Thailand | Other Asia Pacific | Other | World | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 0 | 1835 | 8.96 | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | ... | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN |
| 1 | 1836 | 9.74 | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | ... | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN |
| 2 | 1837 | 10.84 | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | ... | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN |
| 3 | 1838 | 10.52 | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | ... | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN |
| 4 | 1839 | 10.03 | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | ... | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN |
5 rows × 55 columns
# Restrict the data to 1921 - 2014.
wine_per_cap = wine_per_cap[wine_per_cap.Year > 1920.0]
wine_per_cap = wine_per_cap[wine_per_cap.Year < 2015.0]
wine_per_cap['Year'] = wine_per_cap['Year'].astype('object')
# Transpose the data and reset to make the columns the years.
wine = wine_per_cap.T.rename_axis('Country',axis=0).reset_index()
# wine.info()
wine.head()
| Country | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | ... | 170 | 171 | 172 | 173 | 174 | 175 | 176 | 177 | 178 | 179 | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 0 | Year | 1921 | 1922 | 1923 | 1924 | 1925 | 1926 | 1927 | 1928 | 1929 | ... | 2005 | 2006 | 2007 | 2008 | 2009 | 2010 | 2011 | 2012 | 2013 | 2014 |
| 1 | France | 16.27 | 19.61 | 18.52 | 20.3 | 18.85 | 18.66 | 16.63 | 17.26 | 18.88 | ... | 5.79 | 5.79 | 5.71 | 5.46 | 5.46 | 5.38 | 5.63 | 5.46 | 5.3 | 5.22 |
| 2 | Italy | 11.09 | 11.84 | 17.92 | 14.15 | 14.6 | 12.17 | 11.54 | 15.46 | 13.67 | ... | 4.99 | 4.84 | 4.83 | 4.53 | 3.98 | 3.71 | 3.71 | 4.16 | 4.06 | 4.15 |
| 3 | Portugal | NaN | NaN | NaN | 8.18 | 9.0 | 7.55 | 9.85 | 8.99 | 10.48 | ... | 6.76 | 6.59 | 6.18 | 6.17 | 6.12 | 6.37 | 6.35 | 6.73 | 5.14 | 5.14 |
| 4 | Spain | 9.02 | 12.71 | 10.62 | 9.45 | 9.83 | 8.62 | 8.22 | 6.92 | 9.39 | ... | 2.95 | 2.74 | 2.19 | 1.91 | 1.79 | 1.68 | 1.82 | 1.83 | 1.76 | 1.82 |
5 rows × 95 columns
headers = wine.iloc[0].astype('object')
headers.apply(str)
wine = pd.DataFrame(wine.values[1:], columns=headers)
wine.columns = wine.columns.astype(str)
wine.rename(columns={'Year':'Country'}, inplace=True)
wine.head()
| Country | 1921 | 1922 | 1923 | 1924 | 1925 | 1926 | 1927 | 1928 | 1929 | ... | 2005 | 2006 | 2007 | 2008 | 2009 | 2010 | 2011 | 2012 | 2013 | 2014 | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 0 | France | 16.27 | 19.61 | 18.52 | 20.3 | 18.85 | 18.66 | 16.63 | 17.26 | 18.88 | ... | 5.79 | 5.79 | 5.71 | 5.46 | 5.46 | 5.38 | 5.63 | 5.46 | 5.3 | 5.22 |
| 1 | Italy | 11.09 | 11.84 | 17.92 | 14.15 | 14.6 | 12.17 | 11.54 | 15.46 | 13.67 | ... | 4.99 | 4.84 | 4.83 | 4.53 | 3.98 | 3.71 | 3.71 | 4.16 | 4.06 | 4.15 |
| 2 | Portugal | NaN | NaN | NaN | 8.18 | 9.0 | 7.55 | 9.85 | 8.99 | 10.48 | ... | 6.76 | 6.59 | 6.18 | 6.17 | 6.12 | 6.37 | 6.35 | 6.73 | 5.14 | 5.14 |
| 3 | Spain | 9.02 | 12.71 | 10.62 | 9.45 | 9.83 | 8.62 | 8.22 | 6.92 | 9.39 | ... | 2.95 | 2.74 | 2.19 | 1.91 | 1.79 | 1.68 | 1.82 | 1.83 | 1.76 | 1.82 |
| 4 | Austria | 2.0 | 3.03 | 1.67 | 1.73 | 1.2 | 1.58 | 1.71 | 1.75 | 1.71 | ... | 3.44 | 3.54 | 3.72 | 3.31 | 2.89 | 3.67 | 3.63 | 3.68 | 3.5 | 3.75 |
5 rows × 95 columns
wine = wine.replace('NaN', np.nan, regex=True)
Some choropleths for 1964 and 2014.
import plotly
import json
import plotly.express as px
import plotly.graph_objects as go
from plotly.subplots import make_subplots
labels = {'1964':"Wine Consumed per capita, Liters", "locations":"Country"}
fig1a = px.choropleth(wine['1964'], locations=wine.Country, locationmode="country names",
scope="world", color='1964', color_continuous_scale="blues",
title="Wine Consumption, 1964",
hover_name='1964', labels=labels) #, hover_data="debtfree")
fig1a.show()
fig1a.write_html('Wine_Cons_1964.html')
In 1964, France, Portugal, Italy and Spain in Europe consumed by far the most wine. Somewhat surprising to me is that Argentina and Chile also were heavy wine consumers.
labels = {"2014":"Wine Consumed per capita, Liters", "locations":"Country"}
fig1b = px.choropleth(wine['2014'], locations=wine.Country, locationmode="country names",
scope="world", color='2014', color_continuous_scale="blues",
title="Wine Consumption, 2014",
hover_name="2014", labels=labels) #, hover_data="debtfree")
fig1b.show()
fig1b.write_html('Wine_Cons_2014.html')
By 2014, France, Italy and Portugal still lead the way, but there consumption is less than half as much per capita than it was in 1964. Furthermore, the rest of the world has begun to catch up, notably Australia, UK and Germany.
Now, Animate the Wine map to show the changes over time. Much cleanup and testing to get the data ready for that.
# Manipulate the data for animation
wine_cols = wine.columns
wine_cols
Index(['Country', '1921', '1922', '1923', '1924', '1925', '1926', '1927',
'1928', '1929', '1930', '1931', '1932', '1933', '1934', '1935', '1936',
'1937', '1938', '1939', '1940', '1941', '1942', '1943', '1944', '1945',
'1946', '1947', '1948', '1949', '1950', '1951', '1952', '1953', '1954',
'1955', '1956', '1957', '1958', '1959', '1960', '1961', '1962', '1963',
'1964', '1965', '1966', '1967', '1968', '1969', '1970', '1971', '1972',
'1973', '1974', '1975', '1976', '1977', '1978', '1979', '1980', '1981',
'1982', '1983', '1984', '1985', '1986', '1987', '1988', '1989', '1990',
'1991', '1992', '1993', '1994', '1995', '1996', '1997', '1998', '1999',
'2000', '2001', '2002', '2003', '2004', '2005', '2006', '2007', '2008',
'2009', '2010', '2011', '2012', '2013', '2014'],
dtype='object', name=0)
wine_cols = wine_cols[1:]
wine_cols
Index(['1921', '1922', '1923', '1924', '1925', '1926', '1927', '1928', '1929',
'1930', '1931', '1932', '1933', '1934', '1935', '1936', '1937', '1938',
'1939', '1940', '1941', '1942', '1943', '1944', '1945', '1946', '1947',
'1948', '1949', '1950', '1951', '1952', '1953', '1954', '1955', '1956',
'1957', '1958', '1959', '1960', '1961', '1962', '1963', '1964', '1965',
'1966', '1967', '1968', '1969', '1970', '1971', '1972', '1973', '1974',
'1975', '1976', '1977', '1978', '1979', '1980', '1981', '1982', '1983',
'1984', '1985', '1986', '1987', '1988', '1989', '1990', '1991', '1992',
'1993', '1994', '1995', '1996', '1997', '1998', '1999', '2000', '2001',
'2002', '2003', '2004', '2005', '2006', '2007', '2008', '2009', '2010',
'2011', '2012', '2013', '2014'],
dtype='object', name=0)
# Melt the data to put it in long form.
wine_long = pd.melt(wine, id_vars=['Country'])
# wine_long.head(100)
wine_long.rename(columns={0:'Year'}, inplace=True)
wine_long
| Country | Year | value | |
|---|---|---|---|
| 0 | France | 1921 | 16.27 |
| 1 | Italy | 1921 | 11.09 |
| 2 | Portugal | 1921 | NaN |
| 3 | Spain | 1921 | 9.02 |
| 4 | Austria | 1921 | 2.00 |
| ... | ... | ... | ... |
| 5071 | Taiwan | 2014 | 0.09 |
| 5072 | Thailand | 2014 | 0.02 |
| 5073 | Other Asia Pacific | 2014 | 0.01 |
| 5074 | Other | 2014 | NaN |
| 5075 | World | 2014 | 0.41 |
5076 rows × 3 columns
The first attempt below is obviously missing something!
I need the country codes in the data to feed to the choropleth!
px.choropleth(wine_long, locations=wine_long.Country, color="value", hover_name="Country", animation_frame="Year",
color_continuous_scale=px.colors.sequential.Plasma, projection="natural earth")
# HAVE TO GET THE COUNTRY CODES IN THERE!!!
# pycountry convert has a method for pulling in 3 letter country codes!
from pycountry_convert import country_alpha2_to_country_name, country_name_to_country_alpha3
# wine
# wine_long['alpha_3'] = wine_long.Country.apply(lambda x: country_name_to_country_alpha3(x))
wine_long.head()
| Country | Year | value | |
|---|---|---|---|
| 0 | France | 1921 | 16.27 |
| 1 | Italy | 1921 | 11.09 |
| 2 | Portugal | 1921 | NaN |
| 3 | Spain | 1921 | 9.02 |
| 4 | Austria | 1921 | 2.00 |
There's a number of non-country listings in the data - need to be cleaned up.
bad = ['Bel-Lux', 'Other WEM', 'Other ECA', 'Other LAC', 'Other AME', 'Other Asia Pacific', 'Other', 'World']
wine_long_cl = wine_long[~wine_long.Country.isin(bad)]
# wine_long_cl.head(55)
# wine_long_cl.tail(55)
#. Have to change Korea to, I guess, South Korea
# wine_long_cl.replace("Korea", "South Korea")
wine_long_cl['Country'] = wine_long_cl['Country'].replace(['Korea'],'South Korea')
<ipython-input-33-ad224940811e>:5: SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame. Try using .loc[row_indexer,col_indexer] = value instead See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy
# That worked with a warning
# Try again with the add column thingy
wine_long_cl['alpha_3'] = wine_long_cl.Country.apply(lambda x: country_name_to_country_alpha3(x))
<ipython-input-36-074b0a36b618>:1: SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame. Try using .loc[row_indexer,col_indexer] = value instead See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy
wine_long_cl
| Country | Year | value | alpha_3 | |
|---|---|---|---|---|
| 0 | France | 1921 | 16.27 | FRA |
| 1 | Italy | 1921 | 11.09 | ITA |
| 2 | Portugal | 1921 | NaN | PRT |
| 3 | Spain | 1921 | 9.02 | ESP |
| 4 | Austria | 1921 | 2.00 | AUT |
| ... | ... | ... | ... | ... |
| 5068 | Malaysia | 2014 | 0.04 | MYS |
| 5069 | Philippines | 2014 | 0.01 | PHL |
| 5070 | Singapore | 2014 | NaN | SGP |
| 5071 | Taiwan | 2014 | 0.09 | TWN |
| 5072 | Thailand | 2014 | 0.02 | THA |
4324 rows × 4 columns
# Get a min and max on the value, to set the range color on the choropleth.
wine_long_cl.describe()
| value | |
|---|---|
| count | 3891.000000 |
| mean | 2.229995 |
| std | 3.200784 |
| min | -1.150000 |
| 25% | 0.070000 |
| 50% | 0.800000 |
| 75% | 3.290000 |
| max | 20.380000 |
labels = {'value':"Wine Consumed per capita, Liters", "locations":"Country"}
fig1_an = px.choropleth(wine_long_cl, locations='alpha_3', color="value", hover_name="Country", animation_frame="Year",
title="Wine Consumption, 1921 - 2014", color_continuous_scale=px.colors.sequential.Plasma,
range_color=[0,21], projection="natural earth", labels=labels)
fig1_an.show()
fig1_an.write_html('Wine_Consumption_1921-2014.html')
Much better!
Below, I create another figure without the title for use in DASH.
# Do it again without the title for use later
labels = {'value':"Wine Consumed per capita, Liters", "locations":"Country"}
fig1_an2 = px.choropleth(wine_long_cl, locations='alpha_3', color="value", hover_name="Country", animation_frame="Year",
color_continuous_scale=px.colors.sequential.Plasma,
range_color=[0,21], projection="natural earth", labels=labels)
Let's look at beer!
I'll try to consolidate the individual steps from above to compact it.
beer_per_cap = pd.read_csv('Beer_per_cap.csv', skiprows=1)
beer_per_cap = beer_per_cap[beer_per_cap.Year > 1920.0]
beer_per_cap = beer_per_cap[beer_per_cap.Year < 2015.0]
beer_per_cap['Year'] = beer_per_cap['Year'].astype('object')
beer = beer_per_cap.T.rename_axis('Country',axis=0).reset_index()
# beer
headers = beer.iloc[0].astype('object')
headers.apply(str)
beer = pd.DataFrame(beer.values[1:], columns=headers)
beer.columns = beer.columns.astype(str)
beer.rename(columns={'Year':'Country'}, inplace=True)
beer = beer.replace('NaN', np.nan, regex=True)
# Now a couple plots for 1964 and 2014
labels = {'1964':"Beer Consumed per capita, Liters", "locations":"Country"}
fig1 = px.choropleth(beer['1964'], locations=beer.Country, locationmode="country names",
scope="world", color='1964', color_continuous_scale="blues",
title="Beer Consumption, 1964",
hover_name='1964', labels=labels) #, hover_data="debtfree")
fig1.show()
fig1.write_html('Beer_Cons_1964.html')
Not really a surprise, but Germany, Australia and the UK led the way with beer consumption in 1964, followed closely by North America.
labels = {'2014':"Beer Consumed per capita, Liters", "locations":"Country"}
fig1 = px.choropleth(beer['2014'], locations=beer.Country, locationmode="country names",
scope="world", color='2014', color_continuous_scale="blues",
title="Beer Consumption, 2014",
hover_name='2014', labels=labels) #, hover_data="debtfree")
fig1.show()
fig1.write_html('Beer_Cons_2014.html')
By 2014, beer is much more widely consumed.
# And now for the Animation
beer_long = pd.melt(beer, id_vars=['Country'])
# beer_long
beer_long.rename(columns={0:'Year'}, inplace=True)
from pycountry_convert import country_alpha2_to_country_name, country_name_to_country_alpha3
# beer_long['alpha_3'] = beer_long.Country.apply(lambda x: country_name_to_country_alpha3(x))
# Failed for the same Non-country entries.
bad = ['Bel-Lux', 'Other WEM', 'Other ECA', 'Other LAC', 'Other AME', 'Other Asia Pacific', 'Other', 'World']
beer_long = beer_long[~beer_long.Country.isin(bad)]
beer_long['Country'] = beer_long['Country'].replace(['Korea'],'South Korea')
beer_long['alpha_3'] = beer_long.Country.apply(lambda x: country_name_to_country_alpha3(x))
# What range do we need for the data?
beer_long.describe()
| value | |
|---|---|
| count | 2719.000000 |
| mean | 2.137918 |
| std | 1.779248 |
| min | 0.000000 |
| 25% | 0.600000 |
| 50% | 1.700000 |
| 75% | 3.400000 |
| max | 7.800000 |
labels = {'value':"Beer Consumed per capita, Liters", "locations":"Country"}
fig2_an = px.choropleth(beer_long, locations='alpha_3', color="value", hover_name="Country", animation_frame="Year",
title="Beer Consumption, 1921 - 2014", color_continuous_scale=px.colors.sequential.Plasma,
range_color=[0,8], projection="natural earth", labels=labels)
fig2_an.show()
fig2_an.write_html('Beer_Consumption_1921-2014.html')
# Again, without the title for later
labels = {'value':"Beer Consumed per capita, Liters", "locations":"Country"}
fig2_an2 = px.choropleth(beer_long, locations='alpha_3', color="value", hover_name="Country", animation_frame="Year",
color_continuous_scale=px.colors.sequential.Plasma,
range_color=[0,8], projection="natural earth", labels=labels)
Now a glimpse into Spirits Consumption
# Now on to Liquor
spir_per_cap = pd.read_csv('Spir_per_cap.csv', skiprows=1)
spir_per_cap = spir_per_cap[spir_per_cap.Year > 1920.0]
spir_per_cap = spir_per_cap[spir_per_cap.Year < 2015.0]
spir_per_cap['Year'] = spir_per_cap['Year'].astype('object')
spir = spir_per_cap.T.rename_axis('Country',axis=0).reset_index()
headers = spir.iloc[0].astype('object')
headers.apply(str)
spir = pd.DataFrame(spir.values[1:], columns=headers)
spir.columns = spir.columns.astype(str)
spir.rename(columns={'Year':'Country'}, inplace=True)
spir = spir.replace('NaN', np.nan, regex=True)
# Now a couple plots for 1964 and 2014
labels = {'1964':"Spirits Consumed per capita, Liters", "locations":"Country"}
fig1 = px.choropleth(spir['1964'], locations=spir.Country, locationmode="country names",
scope="world", color='1964', color_continuous_scale="blues",
title="Spirit Consumption, 1964",
hover_name='1964', labels=labels) #, hover_data="debtfree")
fig1.show()
fig1.write_html('Spirits_Cons_1964.html')
I was somewhat surprised to see Russia leading the way in Spirits consumption.
labels = {'2014':"Spirits Consumed per capita, Liters", "locations":"Country"}
fig1 = px.choropleth(spir['2014'], locations=spir.Country, locationmode="country names",
scope="world", color='2014', color_continuous_scale="blues",
title="Spirit Consumption, 2014",
hover_name='2014', labels=labels) #, hover_data="debtfree")
fig1.show()
fig1.write_html('Spirits_Cons_2014.html')
Bulgaria and Thailand nudge out Russia for cunsumption of spirits.
# And now the Animation over the years
spir_long = pd.melt(spir, id_vars=['Country'])
spir_long.rename(columns={0:'Year'}, inplace=True)
bad = ['Bel-Lux', 'Other WEM', 'Other ECA', 'Other LAC', 'Other AME', 'Other Asia Pacific', 'Other', 'World']
spir_long = spir_long[~spir_long.Country.isin(bad)]
spir_long['Country'] = spir_long['Country'].replace(['Korea'],'South Korea')
spir_long['alpha_3'] = spir_long.Country.apply(lambda x: country_name_to_country_alpha3(x))
# What range do we need for the data?
spir_long.describe()
| value | |
|---|---|
| count | 2559.000000 |
| mean | 1.552150 |
| std | 1.123486 |
| min | 0.000000 |
| 25% | 0.700000 |
| 50% | 1.400000 |
| 75% | 2.200000 |
| max | 7.000000 |
labels = {'value':"Spirits Consumed per capita, Liters", "locations":"Country"}
fig3_an = px.choropleth(spir_long, locations='alpha_3', color="value", hover_name="Country", animation_frame="Year",
title="Spirit Consumption, 1921 - 2014", color_continuous_scale=px.colors.sequential.Plasma,
range_color=[0,7], projection="natural earth", labels=labels)
fig3_an.show()
fig3_an.write_html('Spirit_Consumption_1921-2014.html')
# Again, without the title
labels = {'value':"Spirits Consumed per capita, Liters", "locations":"Country"}
fig3_an2 = px.choropleth(spir_long, locations='alpha_3', color="value", hover_name="Country", animation_frame="Year",
color_continuous_scale=px.colors.sequential.Plasma,
range_color=[0,7], projection="natural earth", labels=labels)
Below is the first attempts to get DASH to work. Commented out right now.
# Dash?
# ! conda install jupyter-dash -y
# ! conda install -c plotly jupyterlab-dash -y
# from jupyter_dash import JupyterDash
# import dash_core_components as dcc
# import dash_html_components as html
# from dash.dependencies import Input, Output
# Combine them?
# import plotly.offline as pyo
# import plotly.graph_objs as go
# import plotly as py
# # fig1 = go.Scatter(y=[1,2,3])
# # fig2 = go.Scatter(y=[3,2,1])
# # plots = [fig1_an, fig2_an, fig3_an]
# plots = [fig1a, fig1b]
# app = JupyterDash()
# layout = html.Div(
# [html.Div(plots[i]) for i in range(len(plots))], # style=col_style[i]
# style = {'margin-right': '0px'}
# )
# app.layout = layout
# # app.run_server(mode='inline') # port=8052, use_reloader=False) #
# app = dash.Dash(__name__, external_stylesheets=external_stylesheets)
# app = JupyterDash(__name__,) # external_stylesheets=external_stylesheets)
# if __name__ == '__main__':
# app.run_server(mode='inline') # , port=8052, debug=True)
# Buttons in Plotly
# import plotly.graph_objects as go
# import pandas as pd
# # load dataset
# df = pd.read_csv("https://raw.githubusercontent.com/plotly/datasets/master/volcano.csv")
# # create figure
# fig = go.Figure()
# # Add surface trace
# fig.add_trace(go.Surface(z=df.values.tolist(), colorscale="Viridis"))
# # Update plot sizing
# fig.update_layout(
# width=800,
# height=900,
# autosize=False,
# margin=dict(t=0, b=0, l=0, r=0),
# template="plotly_white",
# )
# # Update 3D scene options
# fig.update_scenes(
# aspectratio=dict(x=1, y=1, z=0.7),
# aspectmode="manual"
# )
# # Add dropdown
# fig.update_layout(
# updatemenus=[
# dict(
# type = "buttons",
# direction = "left",
# buttons=list([
# dict(
# args=["type", "surface"],
# label="3D Surface",
# method="restyle"
# ),
# dict(
# args=["type", "heatmap"],
# label="Heatmap",
# method="restyle"
# )
# ]),
# pad={"r": 10, "t": 10},
# showactive=True,
# x=0.11,
# xanchor="left",
# y=1.1,
# yanchor="top"
# ),
# ]
# )
# # Add annotation
# fig.update_layout(
# annotations=[
# dict(text="Trace type:", showarrow=False,
# x=0, y=1.08, yref="paper", align="left")
# ]
# )
# fig.show()
import plotly.graph_objects as go
import pandas as pd
# load dataset
# df = pd.read_csv("https://raw.githubusercontent.com/plotly/datasets/master/volcano.csv")
# create figure
# fig = fig1_an # go.Figure()
# fig2 = fig2_an
# fig3 = fig3_an
# # # Add surface trace
# # fig.add_trace(go.Surface(z=df.values.tolist(), colorscale="Viridis"))
# # Update plot sizing
# fig.update_layout(
# width=800,
# height=900,
# autosize=False,
# margin=dict(t=0, b=0, l=0, r=0),
# template="plotly_white",
# )
# Update 3D scene options
# fig.update_scenes(
# aspectratio=dict(x=1, y=1, z=0.7),
# aspectmode="manual"
# )
# Add dropdown
# fig.update_layout(
# updatemenus=[
# dict(
# type = "buttons",
# direction = "left",
# buttons=list([
# dict(
# args=["type", "choropleth"],
# label="Wine",
# method="update"
# ),
# dict(
# args=["type", "choropleth"],
# label="Beer",
# method="update"
# )
# ]),
# pad={"r": 10, "t": 10},
# showactive=True,
# x=0.11,
# xanchor="left",
# y=1.1,
# yanchor="top"
# ),
# ]
# )
# data = [fig, fig2]
# updatemenus = list([
# dict(active=-1,
# buttons=list([
# dict(label = 'High',
# method = 'update',
# args = [{'visible': [True, False]},
# {'title': 'Yahoo High'}]),
# dict(label = 'Low',
# method = 'update',
# args = [{'visible': [False, True]},
# {'title': 'Yahoo Low'}])
# ]),
# )
# ])
# # Add annotation
# # fig.update_layout(
# # annotations=[
# # dict(text="Alcohol Beverage Consumed per capita", showarrow=False,
# # x=0, y=1.07, yref="paper", align="left")
# # ]
# # )
# fig.show()
# Another attempt
# data = [fig1a,fig1b]
# updatemenus = list([
# dict(active=0,
# showactive = True,
# buttons=list([
# dict(label = "Wine",
# method = "update",
# args = [{"visible": [True, False]}]), # hide trace2
# dict(label = "Beer",
# method = "update",
# args = [{"visible": [False, True]}]) # hide trace1
# ]))])
# updatemenus = list([
# dict(active=0,
# buttons=list([
# dict(label = "4 Aug 1",
# method = "update",
# args= [data[0]]),
# dict(label = "4 Aug 2",
# method = "update",
# args= [data[1]])]))])
# layout = dict(title="Dropdown",
# showlegend=True,
# xaxis=dict(title="Hours"),
# yaxis=dict(title="Number"),
# updatemenus=updatemenus)
# fig=dict(data=data, layout=layout, type='choropleth')
# plotly.offline.plot(fig)
The following attempt at using DASH works!
Again, as noted, I used the interactive JupyterDash, and there was no way to save just the HTML. DASH uses javascript extensively and does not include the data in the HTML pieces.
# Another attempt at DASH
from jupyter_dash import JupyterDash
import dash
from jupyter_dash import JupyterDash
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output
import plotly.express as px
# df = px.data.election()
# geojson = px.data.election_geojson()
# candidates = df.winner.unique()
alc_type = ['Wine', 'Beer', 'Spirits']
app1 = JupyterDash(__name__)
app1.layout = html.Div([
html.P("Type of Alcohol Consumed per capita:"),
dcc.RadioItems(
id='alc_type',
options=[{'value': x, 'label': x}
for x in alc_type],
value=alc_type[0],
labelStyle={'display': 'inline-block'}
),
dcc.Graph(id="choropleth"),
])
@app1.callback(
Output("choropleth", "figure"),
[Input("alc_type", "value")])
def display_choropleth(candidate):
if candidate == "Wine":
figWBS = fig1_an2
if candidate == "Beer":
figWBS = fig2_an2
if candidate == "Spirits":
figWBS = fig3_an2
# fig.update_geos(fitbounds="locations", visible=True)
# fig.update_layout(margin={"r":0,"t":0,"l":0,"b":0})
return figWBS
app1.run_server(debug=False, mode='inline') #, port=8052) #use_reloader=False) port=8052, use_reloader=False) #
--------------------------------------------------------------------------- UnboundLocalError Traceback (most recent call last) <ipython-input-137-94db3a962183> in display_choropleth(candidate='White') 40 # fig.update_layout(margin={"r":0,"t":0,"l":0,"b":0}) 41 ---> 42 return figWBS figWBS = undefined 43 44 app1.run_server(debug=False, mode='inline') #, port=8052) #use_reloader=False) port=8052, use_reloader=False) # UnboundLocalError: local variable 'figWBS' referenced before assignment --------------------------------------------------------------------------- UnboundLocalError Traceback (most recent call last) <ipython-input-137-94db3a962183> in display_choropleth(candidate='Red') 40 # fig.update_layout(margin={"r":0,"t":0,"l":0,"b":0}) 41 ---> 42 return figWBS figWBS = undefined 43 44 app1.run_server(debug=False, mode='inline') #, port=8052) #use_reloader=False) port=8052, use_reloader=False) # UnboundLocalError: local variable 'figWBS' referenced before assignment
I decided to look at Wine grape growth by Country by Hectare. Who grows the most red and white grapes globally? For now, data is just for 2016.
red_wine_growth = pd.read_csv('Red wine growth area.csv', skiprows=1, header=0)
red_wine_growth.head()
| Unnamed: 0 | 2000 | 2010 | 2016 | Hectares | % | 2000.1 | 2010.1 | 2016.1 | |
|---|---|---|---|---|---|---|---|---|---|
| 0 | Algeria | 30200.0 | 30200.0 | 8000.0 | -22200.0 | -74.0 | 100.0 | 100.0 | 96.0 |
| 1 | Argentina | 105606.0 | 125271.0 | 129381.0 | 23775.0 | 23.0 | 53.0 | 59.0 | 63.0 |
| 2 | Armenia | NaN | NaN | 4405.0 | NaN | NaN | NaN | NaN | 30.0 |
| 3 | Australia | 77372.0 | 92200.0 | 84141.0 | 6768.0 | 9.0 | 59.0 | 61.0 | 64.0 |
| 4 | Austria | 12977.0 | 16137.0 | 15306.0 | 2329.0 | 18.0 | 27.0 | 35.0 | 34.0 |
# red_wine_growth['2016']
rwg = red_wine_growth.rename(columns = {'Unnamed: 0': 'Country'}, inplace = False)
rwg.head()
| Country | 2000 | 2010 | 2016 | Hectares | % | 2000.1 | 2010.1 | 2016.1 | |
|---|---|---|---|---|---|---|---|---|---|
| 0 | Algeria | 30200.0 | 30200.0 | 8000.0 | -22200.0 | -74.0 | 100.0 | 100.0 | 96.0 |
| 1 | Argentina | 105606.0 | 125271.0 | 129381.0 | 23775.0 | 23.0 | 53.0 | 59.0 | 63.0 |
| 2 | Armenia | NaN | NaN | 4405.0 | NaN | NaN | NaN | NaN | 30.0 |
| 3 | Australia | 77372.0 | 92200.0 | 84141.0 | 6768.0 | 9.0 | 59.0 | 61.0 | 64.0 |
| 4 | Austria | 12977.0 | 16137.0 | 15306.0 | 2329.0 | 18.0 | 27.0 | 35.0 | 34.0 |
# Let's get the white grape file
white_wine_growth = pd.read_csv('White wine grape growth area.csv', skiprows=1, header=0)
white_wine_growth.head()
| Unnamed: 0 | 2000 | 2010 | 2016 | Hectares | % | 2000.1 | 2010.1 | 2016.1 | |
|---|---|---|---|---|---|---|---|---|---|
| 0 | Algeria | NaN | NaN | 300.0 | NaN | NaN | NaN | NaN | 4.0 |
| 1 | Argentina | 60656.0 | 57591.0 | 47671.0 | -12985.0 | -21.0 | 31.0 | 27.0 | 23.0 |
| 2 | Armenia | 11206.0 | 11206.0 | 10300.0 | -906.0 | -8.0 | 100.0 | 100.0 | 70.0 |
| 3 | Australia | 53230.0 | 56292.0 | 44641.0 | -8589.0 | -16.0 | 41.0 | 37.0 | 34.0 |
| 4 | Austria | 34840.0 | 28890.0 | 29623.0 | -5217.0 | -15.0 | 72.0 | 63.0 | 65.0 |
wwg = white_wine_growth.rename(columns = {'Unnamed: 0': 'Country'}, inplace = False)
wwg.head()
| Country | 2000 | 2010 | 2016 | Hectares | % | 2000.1 | 2010.1 | 2016.1 | |
|---|---|---|---|---|---|---|---|---|---|
| 0 | Algeria | NaN | NaN | 300.0 | NaN | NaN | NaN | NaN | 4.0 |
| 1 | Argentina | 60656.0 | 57591.0 | 47671.0 | -12985.0 | -21.0 | 31.0 | 27.0 | 23.0 |
| 2 | Armenia | 11206.0 | 11206.0 | 10300.0 | -906.0 | -8.0 | 100.0 | 100.0 | 70.0 |
| 3 | Australia | 53230.0 | 56292.0 | 44641.0 | -8589.0 | -16.0 | 41.0 | 37.0 | 34.0 |
| 4 | Austria | 34840.0 | 28890.0 | 29623.0 | -5217.0 | -15.0 | 72.0 | 63.0 | 65.0 |
127.0.0.1 - - [06/Jun/2021 21:06:27] "GET / HTTP/1.1" 200 -
# Clean up the non-country entries in each df
rwg = rwg[:53]
wwg = wwg[:53]
rwg
| Country | 2000 | 2010 | 2016 | Hectares | % | 2000.1 | 2010.1 | 2016.1 | |
|---|---|---|---|---|---|---|---|---|---|
| 0 | Algeria | 30200.0 | 30200.0 | 8000.0 | -22200.0 | -74.0 | 100.0 | 100.0 | 96.0 |
| 1 | Argentina | 105606.0 | 125271.0 | 129381.0 | 23775.0 | 23.0 | 53.0 | 59.0 | 63.0 |
| 2 | Armenia | NaN | NaN | 4405.0 | NaN | NaN | NaN | NaN | 30.0 |
| 3 | Australia | 77372.0 | 92200.0 | 84141.0 | 6768.0 | 9.0 | 59.0 | 61.0 | 64.0 |
| 4 | Austria | 12977.0 | 16137.0 | 15306.0 | 2329.0 | 18.0 | 27.0 | 35.0 | 34.0 |
| 5 | Brazil | 31721.0 | 40874.0 | 27998.0 | -3722.0 | -12.0 | 60.0 | 83.0 | 84.0 |
| 6 | Bulgaria | 58273.0 | 35543.0 | 31196.0 | -27077.0 | -46.0 | 61.0 | 63.0 | 59.0 |
| 7 | Cambodia | NaN | NaN | 10.0 | NaN | NaN | NaN | NaN | 100.0 |
| 8 | Canada | 4740.0 | 4548.0 | 5272.0 | 531.0 | 11.0 | 56.0 | 45.0 | 42.0 |
| 9 | Chile | 86936.0 | 81017.0 | 101275.0 | 14339.0 | 16.0 | 76.0 | 73.0 | 69.0 |
| 10 | China | NaN | 28350.0 | 152353.0 | NaN | NaN | NaN | 96.0 | 86.0 |
| 11 | Croatia | 15084.0 | 7085.0 | 4405.0 | -10679.0 | -71.0 | 25.0 | 34.0 | 38.0 |
| 12 | Cyprus | 14625.0 | 5707.0 | 3187.0 | -11438.0 | -78.0 | 80.0 | 66.0 | 62.0 |
| 13 | Czechia | 3399.0 | 5556.0 | 4392.0 | 993.0 | 29.0 | 30.0 | 34.0 | 32.0 |
| 14 | Ethiopia | NaN | 111.0 | 111.0 | NaN | NaN | NaN | 66.0 | 66.0 |
| 15 | France | 596377.0 | 561527.0 | 534071.0 | -62306.0 | -10.0 | 69.0 | 67.0 | 66.0 |
| 16 | Georgia | 4563.0 | 5853.0 | 5853.0 | 1290.0 | 28.0 | 12.0 | 12.0 | 12.0 |
| 17 | Germany | 25180.0 | 36792.0 | 34175.0 | 8995.0 | 36.0 | 24.0 | 36.0 | 36.0 |
| 18 | Greece | 18901.0 | 23965.0 | 21491.0 | 2590.0 | 14.0 | 37.0 | 44.0 | 42.0 |
| 19 | Hungary | 20939.0 | 21090.0 | 19111.0 | -1829.0 | -9.0 | 24.0 | 30.0 | 30.0 |
| 20 | India | NaN | NaN | 700.0 | NaN | NaN | NaN | NaN | 26.0 |
| 21 | Israel | 2970.0 | 2970.0 | 4148.0 | 1177.0 | 40.0 | 61.0 | 61.0 | 83.0 |
| 22 | Italy | 328902.0 | 356033.0 | 326428.0 | -2474.0 | -1.0 | 52.0 | 57.0 | 54.0 |
| 23 | Japan | NaN | 2213.0 | 1769.0 | NaN | NaN | NaN | 60.0 | 46.0 |
| 24 | Kazakhstan | NaN | 955.0 | 944.0 | NaN | NaN | NaN | 14.0 | 14.0 |
| 25 | Korea, Rep. | 5300.0 | 5300.0 | 5300.0 | 0.0 | 0.0 | 98.0 | 98.0 | 98.0 |
| 26 | Lebanon | NaN | NaN | 2182.0 | NaN | NaN | NaN | NaN | 55.0 |
| 27 | Luxembourg | 67.0 | 101.0 | 126.0 | 59.0 | 89.0 | 5.0 | 8.0 | 10.0 |
| 28 | Mexico | NaN | 3409.0 | 3534.0 | NaN | NaN | NaN | 62.0 | 65.0 |
| 29 | Moldova | 35741.0 | 35741.0 | 42442.0 | 6701.0 | 19.0 | 40.0 | 40.0 | 51.0 |
| 30 | Morocco | 40699.0 | 40165.0 | 8830.0 | -31870.0 | -78.0 | 82.0 | 82.0 | 50.0 |
| 31 | Myanmar | NaN | 44.0 | 40.0 | NaN | NaN | NaN | 59.0 | 56.0 |
| 32 | New Zealand | 2825.0 | 7689.0 | 7851.0 | 5027.0 | 178.0 | 28.0 | 24.0 | 22.0 |
| 33 | North Macedonia | NaN | NaN | 14826.0 | NaN | NaN | NaN | NaN | 60.0 |
| 34 | Norway | NaN | NaN | 4.0 | NaN | NaN | NaN | NaN | 34.0 |
| 35 | Peru | NaN | 2292.0 | 2292.0 | NaN | NaN | NaN | 60.0 | 60.0 |
| 36 | Portugal | 119300.0 | 109783.0 | 118392.0 | -908.0 | -1.0 | 58.0 | 67.0 | 65.0 |
| 37 | Romania | 61576.0 | 52817.0 | 61869.0 | 292.0 | 0.0 | 28.0 | 31.0 | 34.0 |
| 38 | Russia | 10706.0 | 9795.0 | 18936.0 | 8230.0 | 77.0 | 19.0 | 38.0 | 37.0 |
| 39 | Serbia | 21390.0 | 21390.0 | 11970.0 | -9420.0 | -44.0 | 31.0 | 31.0 | 54.0 |
| 40 | Slovakia | 2649.0 | 3723.0 | 3063.0 | 414.0 | 16.0 | 17.0 | 29.0 | 40.0 |
| 41 | Slovenia | 6103.0 | 5282.0 | 4926.0 | -1177.0 | -19.0 | 26.0 | 32.0 | 31.0 |
| 42 | South Africa | 33512.0 | 44049.0 | 42561.0 | 9049.0 | 27.0 | 36.0 | 44.0 | 44.0 |
| 43 | Spain | 460529.0 | 558859.0 | 469385.0 | 8857.0 | 2.0 | 39.0 | 54.0 | 53.0 |
| 44 | Switzerland | 7913.0 | 8574.0 | 8515.0 | 602.0 | 8.0 | 53.0 | 58.0 | 58.0 |
| 45 | Taiwan | 1530.0 | 1530.0 | 94.0 | -1436.0 | -94.0 | 54.0 | 54.0 | 63.0 |
| 46 | Thailand | NaN | 99.0 | 111.0 | NaN | NaN | NaN | 66.0 | 53.0 |
| 47 | Tunisia | 16836.0 | 16836.0 | 1707.0 | -15129.0 | -90.0 | 100.0 | 100.0 | 50.0 |
| 48 | Turkey | NaN | 8677.0 | 9355.0 | NaN | NaN | NaN | 67.0 | 68.0 |
| 49 | Ukraine | NaN | 16149.0 | 9170.0 | NaN | NaN | NaN | 31.0 | 36.0 |
| 50 | United Kingdom | 200.0 | 405.0 | 867.0 | 667.0 | 333.0 | 23.0 | 34.0 | 47.0 |
| 51 | United States | 96153.0 | 143076.0 | 153116.0 | 56963.0 | 59.0 | 55.0 | 63.0 | 64.0 |
| 52 | Uruguay | 8555.0 | 6152.0 | 5476.0 | -3079.0 | -36.0 | 96.0 | 80.0 | 81.0 |
wwg
127.0.0.1 - - [06/Jun/2021 21:06:27] "GET /_dash-layout HTTP/1.1" 200 - 127.0.0.1 - - [06/Jun/2021 21:06:27] "GET /_dash-dependencies HTTP/1.1" 200 -
| Country | 2000 | 2010 | 2016 | Hectares | % | 2000.1 | 2010.1 | 2016.1 | |
|---|---|---|---|---|---|---|---|---|---|
| 0 | Algeria | NaN | NaN | 300.0 | NaN | NaN | NaN | NaN | 4.0 |
| 1 | Argentina | 60656.0 | 57591.0 | 47671.0 | -12985.0 | -21.0 | 31.0 | 27.0 | 23.0 |
| 2 | Armenia | 11206.0 | 11206.0 | 10300.0 | -906.0 | -8.0 | 100.0 | 100.0 | 70.0 |
| 3 | Australia | 53230.0 | 56292.0 | 44641.0 | -8589.0 | -16.0 | 41.0 | 37.0 | 34.0 |
| 4 | Austria | 34840.0 | 28890.0 | 29623.0 | -5217.0 | -15.0 | 72.0 | 63.0 | 65.0 |
| 5 | Brazil | 21119.0 | 8523.0 | 5199.0 | -15921.0 | -75.0 | 40.0 | 17.0 | 16.0 |
| 6 | Bulgaria | 37724.0 | 16431.0 | 17429.0 | -20295.0 | -54.0 | 39.0 | 29.0 | 33.0 |
| 7 | Cambodia | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN |
| 8 | Canada | 3548.0 | 4971.0 | 6637.0 | 3089.0 | 87.0 | 42.0 | 49.0 | 53.0 |
| 9 | Chile | 27028.0 | 30408.0 | 44160.0 | 17133.0 | 63.0 | 24.0 | 27.0 | 30.0 |
| 10 | China | NaN | 1193.0 | 25647.0 | NaN | NaN | NaN | 4.0 | 14.0 |
| 11 | Croatia | 44364.0 | 13394.0 | 7341.0 | -37023.0 | -83.0 | 75.0 | 65.0 | 62.0 |
| 12 | Cyprus | 3656.0 | 2901.0 | 1946.0 | -1710.0 | -47.0 | 20.0 | 34.0 | 38.0 |
| 13 | Czechia | 7932.0 | 9782.0 | 8382.0 | 450.0 | 6.0 | 70.0 | 60.0 | 62.0 |
| 14 | Ethiopia | NaN | 58.0 | 58.0 | NaN | NaN | NaN | 34.0 | 34.0 |
| 15 | France | 263593.0 | 269576.0 | 276551.0 | 12958.0 | 5.0 | 30.0 | 32.0 | 34.0 |
| 16 | Georgia | 32836.0 | 42122.0 | 42121.0 | 9285.0 | 28.0 | 88.0 | 88.0 | 88.0 |
| 17 | Germany | 75241.0 | 59837.0 | 55328.0 | -19913.0 | -26.0 | 72.0 | 59.0 | 59.0 |
| 18 | Greece | 24052.0 | 20174.0 | 18860.0 | -5191.0 | -22.0 | 47.0 | 37.0 | 37.0 |
| 19 | Hungary | 61657.0 | 42335.0 | 38187.0 | -23470.0 | -38.0 | 71.0 | 61.0 | 60.0 |
| 20 | India | NaN | NaN | 2000.0 | NaN | NaN | NaN | NaN | 74.0 |
| 21 | Israel | 1881.0 | 1881.0 | 852.0 | -1029.0 | -55.0 | 39.0 | 39.0 | 17.0 |
| 22 | Italy | 301118.0 | 252293.0 | 259257.0 | -41861.0 | -14.0 | 47.0 | 40.0 | 43.0 |
| 23 | Japan | NaN | 1282.0 | 1155.0 | NaN | NaN | NaN | 35.0 | 30.0 |
| 24 | Kazakhstan | NaN | 5598.0 | 5609.0 | NaN | NaN | NaN | 81.0 | 81.0 |
| 25 | Korea, Rep. | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN |
| 26 | Lebanon | NaN | NaN | 1818.0 | NaN | NaN | NaN | NaN | 45.0 |
| 27 | Luxembourg | 1126.0 | 1057.0 | 978.0 | -148.0 | -13.0 | 84.0 | 81.0 | 75.0 |
| 28 | Mexico | NaN | 2056.0 | 1931.0 | NaN | NaN | NaN | 38.0 | 35.0 |
| 29 | Moldova | 52061.0 | 52061.0 | 38896.0 | -13165.0 | -25.0 | 58.0 | 58.0 | 47.0 |
| 30 | Morocco | 8901.0 | 8835.0 | 8760.0 | -140.0 | -2.0 | 18.0 | 18.0 | 50.0 |
| 31 | Myanmar | NaN | 31.0 | 31.0 | NaN | NaN | NaN | 41.0 | 44.0 |
| 32 | New Zealand | 6984.0 | 22772.0 | 25187.0 | 18203.0 | 261.0 | 70.0 | 71.0 | 71.0 |
| 33 | North Macedonia | NaN | NaN | 9951.0 | NaN | NaN | NaN | NaN | 40.0 |
| 34 | Norway | NaN | NaN | 8.0 | NaN | NaN | NaN | NaN | 66.0 |
| 35 | Peru | NaN | 1532.0 | 1532.0 | NaN | NaN | NaN | 40.0 | 40.0 |
| 36 | Portugal | 85703.0 | 53704.0 | 57011.0 | -28691.0 | -33.0 | 42.0 | 33.0 | 31.0 |
| 37 | Romania | 158209.0 | 115899.0 | 118982.0 | -39227.0 | -25.0 | 71.0 | 68.0 | 65.0 |
| 38 | Russia | 45626.0 | 15755.0 | 31780.0 | -13846.0 | -30.0 | 81.0 | 61.0 | 63.0 |
| 39 | Serbia | 47609.0 | 47609.0 | 9932.0 | -37677.0 | -79.0 | 69.0 | 69.0 | 45.0 |
| 40 | Slovakia | 12932.0 | 8671.0 | 4685.0 | -8246.0 | -64.0 | 83.0 | 69.0 | 60.0 |
| 41 | Slovenia | 17369.0 | 10571.0 | 10555.0 | -6814.0 | -39.0 | 74.0 | 65.0 | 66.0 |
| 42 | South Africa | 60040.0 | 56704.0 | 52834.0 | -7205.0 | -12.0 | 64.0 | 56.0 | 55.0 |
| 43 | Spain | 721155.0 | 469322.0 | 412658.0 | -308497.0 | -43.0 | 61.0 | 46.0 | 47.0 |
| 44 | Switzerland | 6979.0 | 6029.0 | 6045.0 | -934.0 | -13.0 | 46.0 | 41.0 | 41.0 |
| 45 | Taiwan | 1303.0 | 1303.0 | 55.0 | -1248.0 | -96.0 | 46.0 | 46.0 | 37.0 |
| 46 | Thailand | NaN | 44.0 | 91.0 | NaN | NaN | NaN | 30.0 | 44.0 |
| 47 | Tunisia | NaN | NaN | 1693.0 | NaN | NaN | NaN | NaN | 50.0 |
| 48 | Turkey | NaN | 4179.0 | 4349.0 | NaN | NaN | NaN | 33.0 | 32.0 |
| 49 | Ukraine | NaN | 35458.0 | 15996.0 | NaN | NaN | NaN | 68.0 | 64.0 |
| 50 | United Kingdom | 673.0 | 734.0 | 919.0 | 246.0 | 37.0 | 77.0 | 61.0 | 50.0 |
| 51 | United States | 78616.0 | 79566.0 | 78987.0 | 370.0 | 0.0 | 45.0 | 35.0 | 33.0 |
| 52 | Uruguay | 325.0 | 1493.0 | 1258.0 | 933.0 | 287.0 | 4.0 | 19.0 | 19.0 |
labels = {'2016':"Red grape production, Hectares", "locations":"Country"}
fig_r_prod = px.choropleth(rwg['2016'], locations=rwg.Country, locationmode="country names",
scope="world", color='2016', color_continuous_scale="viridis",
title="Red Wine Grape Production, 2016",
hover_name='2016', labels=labels) #, hover_data="debtfree")
fig_r_prod.show()
fig_r_prod.write_html('red_grape_2016_hectares.html')
127.0.0.1 - - [06/Jun/2021 21:06:27] "POST /_dash-update-component HTTP/1.1" 200 -
Probably not surprisingly, France, Spain and Italy lead the way in red grape production, with production in the US, China and Argentina running at about half as much.
labels = {'2016':"White grape production, Hectares", "locations":"Country"}
fig_w_prod = px.choropleth(wwg['2016'], locations=wwg.Country, locationmode="country names",
scope="world", color='2016', color_continuous_scale="viridis",
title="White Wine Grape Production, 2016",
hover_name='2016', labels=labels) #, hover_data="debtfree")
fig_w_prod.show()
fig_w_prod.write_html('white_grape_2016_hectares.html')
Spain leads the way for white grape growth in hectares. For the record, a "hectare" is approximately 2.5 acres.
# Dash selection for these two
import dash
from jupyter_dash import JupyterDash
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output
import plotly.express as px
grape_type = ['Red', 'White']
app2 = JupyterDash(__name__)
app2.layout = html.Div([
html.P("Grape Production by Hectare, 2016:"),
dcc.RadioItems(
id='grape_type',
options=[{'value': x, 'label': x}
for x in grape_type],
value=grape_type[0],
labelStyle={'display': 'inline-block'}
),
dcc.Graph(id="choropleth"),
])
@app2.callback(
Output("choropleth", "figure"),
[Input("grape_type", "value")])
def display_choropleth(candidate):
if candidate == "Red":
figRW = fig_r_prod
if candidate == "White":
figRW = fig_w_prod
# fig.update_geos(fitbounds="locations", visible=True)
# fig.update_layout(margin={"r":0,"t":0,"l":0,"b":0})
return figRW
app2.run_server(debug=False, mode='inline') #, port=8052) #use_reloader=False) port=8052, use_reloader=False) #
--------------------------------------------------------------------------- UnboundLocalError Traceback (most recent call last) <ipython-input-136-1a87c6946bd2> in display_choropleth(candidate='Beer') 35 # fig.update_layout(margin={"r":0,"t":0,"l":0,"b":0}) 36 ---> 37 return figRW figRW = undefined 38 39 app2.run_server(debug=False, mode='inline') #, port=8052) #use_reloader=False) port=8052, use_reloader=False) # UnboundLocalError: local variable 'figRW' referenced before assignment --------------------------------------------------------------------------- UnboundLocalError Traceback (most recent call last) <ipython-input-136-1a87c6946bd2> in display_choropleth(candidate='Spirits') 35 # fig.update_layout(margin={"r":0,"t":0,"l":0,"b":0}) 36 ---> 37 return figRW figRW = undefined 38 39 app2.run_server(debug=False, mode='inline') #, port=8052) #use_reloader=False) port=8052, use_reloader=False) # UnboundLocalError: local variable 'figRW' referenced before assignment --------------------------------------------------------------------------- UnboundLocalError Traceback (most recent call last) <ipython-input-136-1a87c6946bd2> in display_choropleth(candidate='Wine') 35 # fig.update_layout(margin={"r":0,"t":0,"l":0,"b":0}) 36 ---> 37 return figRW figRW = undefined 38 39 app2.run_server(debug=False, mode='inline') #, port=8052) #use_reloader=False) port=8052, use_reloader=False) # UnboundLocalError: local variable 'figRW' referenced before assignment
Here, I began playing with types of red grapes, and where they are grown.
top_red = pd.read_csv('Top24RedGrapes.csv', skiprows=2, header=0)
top_red.head()
| Unnamed: 0 | Unnamed: 1 | Country | bearing area (ha) | % of world | |
|---|---|---|---|---|---|
| 0 | Cabernet Sauvignon | 1 | France | 46555.0 | 15.0 |
| 1 | Cabernet Sauvignon | 2 | Chile | 42409.0 | 13.7 |
| 2 | Cabernet Sauvignon | 3 | United States | 40837.0 | 13.1 |
| 3 | Cabernet Sauvignon | 4 | China | 40300.0 | 13.0 |
| 4 | Cabernet Sauvignon | 5 | Australia | 23987.0 | 7.7 |
127.0.0.1 - - [06/Jun/2021 21:06:27] "GET / HTTP/1.1" 200 -
top_red.sample(10)
| Unnamed: 0 | Unnamed: 1 | Country | bearing area (ha) | % of world | |
|---|---|---|---|---|---|
| 136 | Sangiovese | 17 | Switzerland | 0.0 | 0.0 |
| 371 | Douce Noire | 12 | NaN | NaN | NaN |
| 85 | Garnacha Tinta | 6 | Algeria | 2000.0 | 1.3 |
| 208 | Monastrell | 9 | Turkey | 7.0 | 0.0 |
| 96 | Garnacha Tinta | 17 | New Zealand | 1.0 | 0.0 |
| 309 | Gamay Noir | 10 | Australia | 6.0 | 0.0 |
| 39 | Merlot | 20 | Switzerland | 1124.0 | 0.4 |
| 453 | Criolla Grande | 14 | NaN | NaN | NaN |
| 261 | Tribidrag | 2 | Italy | 13896.0 | 41.3 |
| 171 | Cabernet Franc | 12 | China | 600.0 | 1.1 |
This data is probably best represented by pie charts, since it is only 20 countries.
red = top_red.rename(columns = {'Unnamed: 0': 'Grape'}, inplace = False)
red.rename(columns = {'bearing area (ha)': 'Area'}, inplace = True)
red
| Grape | Unnamed: 1 | Country | Area | % of world | |
|---|---|---|---|---|---|
| 0 | Cabernet Sauvignon | 1 | France | 46555.0 | 15.0 |
| 1 | Cabernet Sauvignon | 2 | Chile | 42409.0 | 13.7 |
| 2 | Cabernet Sauvignon | 3 | United States | 40837.0 | 13.1 |
| 3 | Cabernet Sauvignon | 4 | China | 40300.0 | 13.0 |
| 4 | Cabernet Sauvignon | 5 | Australia | 23987.0 | 7.7 |
| ... | ... | ... | ... | ... | ... |
| 475 | Pinot Meunier | 16 | NaN | NaN | NaN |
| 476 | Pinot Meunier | 17 | NaN | NaN | NaN |
| 477 | Pinot Meunier | 18 | NaN | NaN | NaN |
| 478 | Pinot Meunier | 19 | NaN | NaN | NaN |
| 479 | Pinot Meunier | 20 | NaN | NaN | NaN |
480 rows × 5 columns
import plotly.express as px
# red.loc[red['Cabernet Sauvignon'] == 'Cabernet Sauvignon', 'country'] = 'Other countries' # Represent only large countries
fig = px.pie(red.loc[red['Grape'] == 'Cabernet Sauvignon'], values='Area', names='Country', title='Cab Sauv')
fig.show()
fig.write_html('Cab_Sauv_grape_growth.html')
127.0.0.1 - - [06/Jun/2021 21:06:27] "GET /_dash-layout HTTP/1.1" 200 - 127.0.0.1 - - [06/Jun/2021 21:06:27] "GET /_dash-dependencies HTTP/1.1" 200 -
fig = px.pie(red.loc[red['Grape'] == 'Merlot'], values='Area', names='Country', title='Merlot')
fig.show()
fig.write_html('Merlot_grape_growth.html')
127.0.0.1 - - [06/Jun/2021 21:06:28] "POST /_dash-update-component HTTP/1.1" 200 -
fig = px.pie(red.loc[red['Grape'] == 'Bobal'], values='Area', names='Country', title='Bobal')
fig.show()
fig.write_html('Bobal_grape_growth.html')
fig = px.pie(red.loc[red['Grape'] == 'Montepulciano'], values='Area', names='Country', title='Montepulciano')
fig.show()
fig.write_html('Montepulciano_grape_growth.html')
Below begins various attempts to get a drop down list of grape types to display the pie charts.
import dash
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output
import plotly.express as px
red_types = red.Grape.unique()
app3 = JupyterDash(__name__)
app3.layout = html.Div([
html.P("Grape:"),
dcc.Dropdown(
id='this_drop',
value='Cabernet Sauvignon',
options=[{'value': x, 'label': x}
for x in red_types],
clearable=False
),
# html.P("Values:"),
# dcc.Dropdown(
# id='values',
# value='total_bill',
# options=[{'value': x, 'label': x}
# for x in ['total_bill', 'tip', 'size']],
# clearable=False
# ),
dcc.Graph(id="pie-chart")
])
@app3.callback(
Output("pie-chart", "fig"),
[Input("this_drop", "value")])
def generate_chart(value):
red2 = red # copy of df??
# print(value)
# fig = px.pie(red2, names=Area)
# return fig
app3.run_server(debug=True, mode='inline')
/Users/pauloleary/opt/miniconda3/envs/dv_env_geo/lib/python3.9/site-packages/jupyter_dash/jupyter_app.py:139: UserWarning: The 'environ['werkzeug.server.shutdown']' function is deprecated and will be removed in Werkzeug 2.1. 127.0.0.1 - - [06/Jun/2021 21:06:28] "GET /_shutdown_f1830328-bfc1-429c-ac26-96e7043b19f3 HTTP/1.1" 200 -
Trouble getting the above to work. Let me try a work around, to create all the pie charts, then pull them in to DASH.
This was a brute force attempt that got a little closer.
red_types = red.Grape.unique()
red_types
array(['Cabernet Sauvignon', 'Merlot', 'Tempranillo', 'Syrah',
'Garnacha Tinta', 'Pinot Noir', 'Sangiovese', 'Bobal',
'Cabernet Franc', 'Côt', 'Monastrell', 'Mazuelo',
'Alicante Henri Bouschet', 'Tribidrag', 'Montepulciano',
'Gamay Noir', 'Cinsaut', 'Carmenère', 'Douce Noire', 'Barbera',
'Isabella', 'Blaufränkisch', 'Criolla Grande', 'Pinot Meunier'],
dtype=object)
Here, I am trrying to figure out a way to work around the DASH issue, like I did above.
x = 0
gr_list = []
for i in red_types:
fig = px.pie(red.loc[red['Grape'] == i], values='Area', names='Country', title=i)
gr_list.append(fig)
gr_list
[Figure({
'data': [{'domain': {'x': [0.0, 1.0], 'y': [0.0, 1.0]},
'hovertemplate': 'Country=%{label}<br>Area=%{value}<extra></extra>',
'labels': array(['France', 'Chile', 'United States', 'China', 'Australia', 'Spain',
'Argentina', 'Italy', 'South Africa', 'Bulgaria', 'Russia', 'Moldova',
'Romania', 'Ukraine', 'Hungary', 'Portugal', 'Serbia', 'Greece',
'N. Macedonia', 'Algeria'], dtype=object),
'legendgroup': '',
'name': '',
'showlegend': True,
'type': 'pie',
'values': array([46555., 42409., 40837., 40300., 23987., 20139., 15356., 14240., 10589.,
9327., 8528., 8169., 5359., 4935., 2677., 2346., 2111., 1929.,
1020., 1000.])}],
'layout': {'legend': {'tracegroupgap': 0}, 'template': '...', 'title': {'text': 'Cabernet Sauvignon'}}
}),
Figure({
'data': [{'domain': {'x': [0.0, 1.0], 'y': [0.0, 1.0]},
'hovertemplate': 'Country=%{label}<br>Area=%{value}<extra></extra>',
'labels': array(['France', 'Italy', 'United States', 'China', 'Spain', 'Chile',
'Romania', 'Bulgaria', 'Australia', 'Moldova', 'Argentina',
'South Africa', 'Russia', 'Serbia', 'Hungary', 'Ukraine', 'Greece',
'N. Macedonia', 'New Zealand', 'Switzerland'], dtype=object),
'legendgroup': '',
'name': '',
'showlegend': True,
'type': 'pie',
'values': array([108483., 24057., 21251., 16700., 12852., 12057., 11647., 10050.,
8415., 7689., 5632., 5558., 2988., 1968., 1961., 1400.,
1393., 1240., 1239., 1124.])}],
'layout': {'legend': {'tracegroupgap': 0}, 'template': '...', 'title': {'text': 'Merlot'}}
}),
Figure({
'data': [{'domain': {'x': [0.0, 1.0], 'y': [0.0, 1.0]},
'hovertemplate': 'Country=%{label}<br>Area=%{value}<extra></extra>',
'labels': array(['Spain', 'Portugal', 'Argentina', 'Australia', 'France',
'United States', 'Mexico', 'Chile', 'South Africa', 'Romania', 'Israel',
'Brazil', 'Greece', 'New Zealand', 'Italy', 'Canada', 'Turkey',
'Thailand', 'Myanmar', 'Switzerland'], dtype=object),
'legendgroup': '',
'name': '',
'showlegend': True,
'type': 'pie',
'values': array([1.93597e+05, 1.70140e+04, 6.14000e+03, 6.81000e+02, 6.58000e+02,
6.26000e+02, 2.29000e+02, 1.27000e+02, 9.20000e+01, 6.70000e+01,
5.50000e+01, 2.30000e+01, 2.20000e+01, 1.80000e+01, 9.00000e+00,
6.00000e+00, 6.00000e+00, 4.00000e+00, 4.00000e+00, 0.00000e+00])}],
'layout': {'legend': {'tracegroupgap': 0}, 'template': '...', 'title': {'text': 'Tempranillo'}}
}),
Figure({
'data': [{'domain': {'x': [0.0, 1.0], 'y': [0.0, 1.0]},
'hovertemplate': 'Country=%{label}<br>Area=%{value}<extra></extra>',
'labels': array(['France', 'Australia', 'Spain', 'Argentina', 'South Africa',
'United States', 'Chile', 'Italy', 'Portugal', 'Turkey', 'Greece',
'China', 'Algeria', 'Bulgaria', 'Romania', 'India', 'New Zealand',
'Israel', 'Morocco', 'Lebanon'], dtype=object),
'legendgroup': '',
'name': '',
'showlegend': True,
'type': 'pie',
'values': array([62211., 38942., 19488., 12707., 9946., 9083., 7994., 7693., 4017.,
1439., 1042., 1000., 1000., 804., 504., 500., 436., 385.,
347., 300.])}],
'layout': {'legend': {'tracegroupgap': 0}, 'template': '...', 'title': {'text': 'Syrah'}}
}),
Figure({
'data': [{'domain': {'x': [0.0, 1.0], 'y': [0.0, 1.0]},
'hovertemplate': 'Country=%{label}<br>Area=%{value}<extra></extra>',
'labels': array(['France', 'Spain', 'Italy', 'China', 'United States', 'Algeria',
'Australia', 'Morocco', 'South Africa', 'Chile', 'Tunisia', 'Mexico',
'Portugal', 'Turkey', 'Argentina', 'Uruguay', 'New Zealand', 'Canada',
'Switzerland', 'Peru'], dtype=object),
'legendgroup': '',
'name': '',
'showlegend': True,
'type': 'pie',
'values': array([7.8631e+04, 5.4606e+04, 5.4210e+03, 4.0000e+03, 2.2130e+03, 2.0000e+03,
1.4920e+03, 7.8600e+02, 3.4400e+02, 1.8700e+02, 1.5200e+02, 1.4000e+02,
6.0000e+01, 3.3000e+01, 2.2000e+01, 4.0000e+00, 1.0000e+00, 1.0000e+00,
1.0000e+00, 1.0000e+00])}],
'layout': {'legend': {'tracegroupgap': 0}, 'template': '...', 'title': {'text': 'Garnacha Tinta'}}
}),
Figure({
'data': [{'domain': {'x': [0.0, 1.0], 'y': [0.0, 1.0]},
'hovertemplate': 'Country=%{label}<br>Area=%{value}<extra></extra>',
'labels': array(['France', 'United States', 'Germany', 'New Zealand', 'Italy',
'Australia', 'Switzerland', 'Chile', 'Moldova', 'Romania', 'Argentina',
'South Africa', 'Hungary', 'Spain', 'Russia', 'Czechia', 'Canada',
'Serbia', 'Austria', 'United Kingdom'], dtype=object),
'legendgroup': '',
'name': '',
'showlegend': True,
'type': 'pie',
'values': array([31602., 22998., 11184., 5514., 5057., 4806., 4209., 4091., 2366.,
1930., 1866., 1153., 1092., 969., 918., 697., 639., 633.,
614., 546.])}],
'layout': {'legend': {'tracegroupgap': 0}, 'template': '...', 'title': {'text': 'Pinot Noir'}}
}),
Figure({
'data': [{'domain': {'x': [0.0, 1.0], 'y': [0.0, 1.0]},
'hovertemplate': 'Country=%{label}<br>Area=%{value}<extra></extra>',
'labels': array(['Italy', 'Argentina', 'France', 'United States', 'Australia', 'Chile',
'Ethiopia', 'Romania', 'South Africa', 'Turkey', 'New Zealand',
'Canada', 'Brazil', 'Spain', 'Thailand', 'Hungary', 'Switzerland', nan,
nan, nan], dtype=object),
'legendgroup': '',
'name': '',
'showlegend': True,
'type': 'pie',
'values': array([6.8428e+04, 1.8370e+03, 1.5030e+03, 8.2700e+02, 4.3000e+02, 1.5200e+02,
9.0000e+01, 8.8000e+01, 7.0000e+01, 1.8000e+01, 8.0000e+00, 4.0000e+00,
3.0000e+00, 2.0000e+00, 2.0000e+00, 1.0000e+00, 0.0000e+00, nan,
nan, nan])}],
'layout': {'legend': {'tracegroupgap': 0}, 'template': '...', 'title': {'text': 'Sangiovese'}}
}),
Figure({
'data': [{'domain': {'x': [0.0, 1.0], 'y': [0.0, 1.0]},
'hovertemplate': 'Country=%{label}<br>Area=%{value}<extra></extra>',
'labels': array(['Spain', nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan,
nan, nan, nan, nan, nan, nan, nan], dtype=object),
'legendgroup': '',
'name': '',
'showlegend': True,
'type': 'pie',
'values': array([59189., nan, nan, nan, nan, nan, nan, nan, nan,
nan, nan, nan, nan, nan, nan, nan, nan, nan,
nan, nan])}],
'layout': {'legend': {'tracegroupgap': 0}, 'template': '...', 'title': {'text': 'Bobal'}}
}),
Figure({
'data': [{'domain': {'x': [0.0, 1.0], 'y': [0.0, 1.0]},
'hovertemplate': 'Country=%{label}<br>Area=%{value}<extra></extra>',
'labels': array(['France', 'Brazil', 'Italy', 'United States', 'Chile', 'Hungary',
'Argentina', 'South Africa', 'Canada', 'Moldova', 'Spain', 'China',
'Australia', 'Uruguay', 'Bulgaria', 'Israel', 'New Zealand', 'Serbia',
'Romania', 'Austria'], dtype=object),
'legendgroup': '',
'name': '',
'showlegend': True,
'type': 'pie',
'values': array([32327., 6834., 5590., 2199., 1578., 1368., 929., 835., 820.,
756., 680., 600., 328., 266., 240., 110., 109., 79.,
72., 64.])}],
'layout': {'legend': {'tracegroupgap': 0}, 'template': '...', 'title': {'text': 'Cabernet Franc'}}
}),
Figure({
'data': [{'domain': {'x': [0.0, 1.0], 'y': [0.0, 1.0]},
'hovertemplate': 'Country=%{label}<br>Area=%{value}<extra></extra>',
'labels': array(['Argentina', 'France', 'Chile', 'United States', 'Australia',
'South Africa', 'Italy', 'Moldova', 'New Zealand', 'Spain', 'Israel',
'Uruguay', 'Canada', 'Brazil', 'Turkey', 'Switzerland', 'Peru',
'Romania', 'Hungary', nan], dtype=object),
'legendgroup': '',
'name': '',
'showlegend': True,
'type': 'pie',
'values': array([4.0401e+04, 6.1000e+03, 2.2930e+03, 1.6100e+03, 5.1500e+02, 4.5200e+02,
1.7800e+02, 1.6200e+02, 1.2900e+02, 1.1300e+02, 1.1000e+02, 4.3000e+01,
4.1000e+01, 3.0000e+01, 2.1000e+01, 1.5000e+01, 1.0000e+01, 7.0000e+00,
3.0000e+00, nan])}],
'layout': {'legend': {'tracegroupgap': 0}, 'template': '...', 'title': {'text': 'Côt'}}
}),
Figure({
'data': [{'domain': {'x': [0.0, 1.0], 'y': [0.0, 1.0]},
'hovertemplate': 'Country=%{label}<br>Area=%{value}<extra></extra>',
'labels': array(['Spain', 'France', 'Australia', 'United States', 'South Africa',
'Chile', 'Israel', 'Argentina', 'Turkey', 'Romania', 'Canada',
'Switzerland', nan, nan, nan, nan, nan, nan, nan, nan], dtype=object),
'legendgroup': '',
'name': '',
'showlegend': True,
'type': 'pie',
'values': array([4.1303e+04, 8.7540e+03, 7.0400e+02, 5.1500e+02, 4.7300e+02, 1.0200e+02,
5.5000e+01, 1.2000e+01, 7.0000e+00, 3.0000e+00, 1.0000e+00, 0.0000e+00,
nan, nan, nan, nan, nan, nan,
nan, nan])}],
'layout': {'legend': {'tracegroupgap': 0}, 'template': '...', 'title': {'text': 'Monastrell'}}
}),
Figure({
'data': [{'domain': {'x': [0.0, 1.0], 'y': [0.0, 1.0]},
'hovertemplate': 'Country=%{label}<br>Area=%{value}<extra></extra>',
'labels': array(['France', 'Spain', 'Algeria', 'Italy', 'Morocco', 'United States',
'Israel', 'Chile', 'Mexico', 'Portugal', 'Tunisia', 'Turkey',
'South Africa', 'China', 'Argentina', 'Australia', 'Greece', nan, nan,
nan], dtype=object),
'legendgroup': '',
'name': '',
'showlegend': True,
'type': 'pie',
'values': array([3.176e+04, 5.461e+03, 3.000e+03, 1.686e+03, 1.230e+03, 1.086e+03,
9.350e+02, 8.110e+02, 4.480e+02, 2.910e+02, 2.380e+02, 1.300e+02,
1.140e+02, 1.000e+02, 1.300e+01, 8.000e+00, 1.000e+00, nan,
nan, nan])}],
'layout': {'legend': {'tracegroupgap': 0}, 'template': '...', 'title': {'text': 'Mazuelo'}}
}),
Figure({
'data': [{'domain': {'x': [0.0, 1.0], 'y': [0.0, 1.0]},
'hovertemplate': 'Country=%{label}<br>Area=%{value}<extra></extra>',
'labels': array(['Spain', 'Chile', 'Portugal', 'France', 'Morocco', 'Turkey',
'United States', 'Italy', 'Tunisia', 'Argentina', 'Brazil', 'Greece',
'Uruguay', 'Romania', 'Australia', 'Hungary', 'South Africa', 'Canada',
'Switzerland', nan], dtype=object),
'legendgroup': '',
'name': '',
'showlegend': True,
'type': 'pie',
'values': array([1.9294e+04, 6.9080e+03, 4.5470e+03, 2.6070e+03, 9.1900e+02, 5.3200e+02,
3.8000e+02, 2.8600e+02, 1.7800e+02, 1.3500e+02, 1.0100e+02, 6.0000e+01,
2.4000e+01, 2.0000e+01, 1.9000e+01, 1.4000e+01, 7.0000e+00, 0.0000e+00,
0.0000e+00, nan])}],
'layout': {'legend': {'tracegroupgap': 0}, 'template': '...', 'title': {'text': 'Alicante Henri Bouschet'}}
}),
Figure({
'data': [{'domain': {'x': [0.0, 1.0], 'y': [0.0, 1.0]},
'hovertemplate': 'Country=%{label}<br>Area=%{value}<extra></extra>',
'labels': array(['United States', 'Italy', 'N. Macedonia', 'Australia', 'Chile',
'South Africa', 'Romania', 'Canada', 'New Zealand', 'France', 'Spain',
'Argentina', 'Switzerland', nan, nan, nan, nan, nan, nan, nan],
dtype=object),
'legendgroup': '',
'name': '',
'showlegend': True,
'type': 'pie',
'values': array([1.8551e+04, 1.3896e+04, 1.0000e+03, 8.7000e+01, 6.6000e+01, 2.4000e+01,
9.0000e+00, 8.0000e+00, 4.0000e+00, 1.0000e+00, 1.0000e+00, 1.0000e+00,
1.0000e+00, nan, nan, nan, nan, nan,
nan, nan])}],
'layout': {'legend': {'tracegroupgap': 0}, 'template': '...', 'title': {'text': 'Tribidrag'}}
}),
Figure({
'data': [{'domain': {'x': [0.0, 1.0], 'y': [0.0, 1.0]},
'hovertemplate': 'Country=%{label}<br>Area=%{value}<extra></extra>',
'labels': array(['Italy', 'Argentina', 'Australia', 'United States', 'New Zealand',
'Chile', 'Brazil', 'Spain', nan, nan, nan, nan, nan, nan, nan, nan, nan,
nan, nan, nan], dtype=object),
'legendgroup': '',
'name': '',
'showlegend': True,
'type': 'pie',
'values': array([3.2724e+04, 8.2000e+01, 6.0000e+01, 5.8000e+01, 8.0000e+00, 2.0000e+00,
1.0000e+00, 0.0000e+00, nan, nan, nan, nan,
nan, nan, nan, nan, nan, nan,
nan, nan])}],
'layout': {'legend': {'tracegroupgap': 0}, 'template': '...', 'title': {'text': 'Montepulciano'}}
}),
Figure({
'data': [{'domain': {'x': [0.0, 1.0], 'y': [0.0, 1.0]},
'hovertemplate': 'Country=%{label}<br>Area=%{value}<extra></extra>',
'labels': array(['France', 'Switzerland', 'Canada', 'Turkey', 'United States', 'Italy',
'Serbia', 'South Africa', 'New Zealand', 'Australia', 'Brazil',
'Slovenia', 'Hungary', 'Uruguay', 'Chile', 'Argentina', 'Portugal', nan,
nan, nan], dtype=object),
'legendgroup': '',
'name': '',
'showlegend': True,
'type': 'pie',
'values': array([2.4095e+04, 1.3490e+03, 2.7200e+02, 2.2800e+02, 1.2300e+02, 6.4000e+01,
5.4000e+01, 9.0000e+00, 7.0000e+00, 6.0000e+00, 5.0000e+00, 4.0000e+00,
3.0000e+00, 1.0000e+00, 0.0000e+00, 0.0000e+00, 0.0000e+00, nan,
nan, nan])}],
'layout': {'legend': {'tracegroupgap': 0}, 'template': '...', 'title': {'text': 'Gamay Noir'}}
}),
Figure({
'data': [{'domain': {'x': [0.0, 1.0], 'y': [0.0, 1.0]},
'hovertemplate': 'Country=%{label}<br>Area=%{value}<extra></extra>',
'labels': array(['France', 'Morocco', 'South Africa', 'Chile', 'Tunisia', 'Turkey',
'United States', 'Portugal', 'Australia', 'Spain', 'Greece', 'Italy',
'Argentina', nan, nan, nan, nan, nan, nan, nan], dtype=object),
'legendgroup': '',
'name': '',
'showlegend': True,
'type': 'pie',
'values': array([1.593e+04, 3.239e+03, 1.767e+03, 8.480e+02, 6.260e+02, 4.300e+02,
4.500e+01, 1.200e+01, 1.000e+01, 1.000e+01, 4.000e+00, 4.000e+00,
1.000e+00, nan, nan, nan, nan, nan,
nan, nan])}],
'layout': {'legend': {'tracegroupgap': 0}, 'template': '...', 'title': {'text': 'Cinsaut'}}
}),
Figure({
'data': [{'domain': {'x': [0.0, 1.0], 'y': [0.0, 1.0]},
'hovertemplate': 'Country=%{label}<br>Area=%{value}<extra></extra>',
'labels': array(['China', 'Chile', 'Italy', 'Argentina', 'France', 'United States',
'Australia', 'Brazil', 'South Africa', 'Canada', 'Hungary',
'Switzerland', 'Spain', nan, nan, nan, nan, nan, nan, nan], dtype=object),
'legendgroup': '',
'name': '',
'showlegend': True,
'type': 'pie',
'values': array([1.1200e+04, 1.0503e+04, 6.3500e+02, 5.9000e+01, 2.8000e+01, 2.4000e+01,
1.6000e+01, 1.0000e+01, 8.0000e+00, 3.0000e+00, 0.0000e+00, 0.0000e+00,
0.0000e+00, nan, nan, nan, nan, nan,
nan, nan])}],
'layout': {'legend': {'tracegroupgap': 0}, 'template': '...', 'title': {'text': 'Carmenère'}}
}),
Figure({
'data': [{'domain': {'x': [0.0, 1.0], 'y': [0.0, 1.0]},
'hovertemplate': 'Country=%{label}<br>Area=%{value}<extra></extra>',
'labels': array(['Argentina', 'Italy', 'United States', 'France', nan, nan, nan, nan,
nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan],
dtype=object),
'legendgroup': '',
'name': '',
'showlegend': True,
'type': 'pie',
'values': array([19072., 630., 31., 0., nan, nan, nan, nan, nan,
nan, nan, nan, nan, nan, nan, nan, nan, nan,
nan, nan])}],
'layout': {'legend': {'tracegroupgap': 0}, 'template': '...', 'title': {'text': 'Douce Noire'}}
}),
Figure({
'data': [{'domain': {'x': [0.0, 1.0], 'y': [0.0, 1.0]},
'hovertemplate': 'Country=%{label}<br>Area=%{value}<extra></extra>',
'labels': array(['Italy', 'United States', 'Argentina', 'Australia', 'Slovenia',
'South Africa', 'Chile', 'Brazil', 'Canada', 'Switzerland', 'Romania',
'Spain', nan, nan, nan, nan, nan, nan, nan, nan], dtype=object),
'legendgroup': '',
'name': '',
'showlegend': True,
'type': 'pie',
'values': array([1.5006e+04, 2.1310e+03, 4.4400e+02, 1.0200e+02, 9.8000e+01, 3.5000e+01,
5.0000e+00, 2.0000e+00, 1.0000e+00, 0.0000e+00, 0.0000e+00, 0.0000e+00,
nan, nan, nan, nan, nan, nan,
nan, nan])}],
'layout': {'legend': {'tracegroupgap': 0}, 'template': '...', 'title': {'text': 'Barbera'}}
}),
Figure({
'data': [{'domain': {'x': [0.0, 1.0], 'y': [0.0, 1.0]},
'hovertemplate': 'Country=%{label}<br>Area=%{value}<extra></extra>',
'labels': array(['Brazil', 'Moldova', 'Russia', 'Ukraine', 'Uruguay', 'Australia',
'Switzerland', nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan,
nan, nan], dtype=object),
'legendgroup': '',
'name': '',
'showlegend': True,
'type': 'pie',
'values': array([1.1664e+04, 3.4680e+03, 1.3620e+03, 1.2000e+03, 1.0200e+02, 1.5000e+01,
1.0000e+00, nan, nan, nan, nan, nan,
nan, nan, nan, nan, nan, nan,
nan, nan])}],
'layout': {'legend': {'tracegroupgap': 0}, 'template': '...', 'title': {'text': 'Isabella'}}
}),
Figure({
'data': [{'domain': {'x': [0.0, 1.0], 'y': [0.0, 1.0]},
'hovertemplate': 'Country=%{label}<br>Area=%{value}<extra></extra>',
'labels': array(['Hungary', 'Austria', 'Germany', 'Slovakia', 'Czechia', 'Romania',
'Serbia', 'Slovenia', 'Croatia', 'Peru', 'Italy', 'Canada',
'Switzerland', 'United States', 'Australia', nan, nan, nan, nan, nan],
dtype=object),
'legendgroup': '',
'name': '',
'showlegend': True,
'type': 'pie',
'values': array([7.260e+03, 2.808e+03, 1.737e+03, 1.216e+03, 1.143e+03, 7.290e+02,
7.270e+02, 7.090e+02, 5.210e+02, 2.900e+02, 2.800e+01, 5.000e+00,
4.000e+00, 3.000e+00, 1.000e+00, nan, nan, nan,
nan, nan])}],
'layout': {'legend': {'tracegroupgap': 0}, 'template': '...', 'title': {'text': 'Blaufränkisch'}}
}),
Figure({
'data': [{'domain': {'x': [0.0, 1.0], 'y': [0.0, 1.0]},
'hovertemplate': 'Country=%{label}<br>Area=%{value}<extra></extra>',
'labels': array(['Argentina', nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan,
nan, nan, nan, nan, nan, nan, nan], dtype=object),
'legendgroup': '',
'name': '',
'showlegend': True,
'type': 'pie',
'values': array([15596., nan, nan, nan, nan, nan, nan, nan, nan,
nan, nan, nan, nan, nan, nan, nan, nan, nan,
nan, nan])}],
'layout': {'legend': {'tracegroupgap': 0}, 'template': '...', 'title': {'text': 'Criolla Grande'}}
}),
Figure({
'data': [{'domain': {'x': [0.0, 1.0], 'y': [0.0, 1.0]},
'hovertemplate': 'Country=%{label}<br>Area=%{value}<extra></extra>',
'labels': array(['France', 'Germany', 'United Kingdom', 'Moldova', 'Australia',
'United States', 'New Zealand', 'South Africa', 'Argentina', 'Canada',
'Italy', 'Spain', 'Chile', 'Switzerland', nan, nan, nan, nan, nan, nan],
dtype=object),
'legendgroup': '',
'name': '',
'showlegend': True,
'type': 'pie',
'values': array([1.213e+04, 2.002e+03, 2.020e+02, 1.380e+02, 8.200e+01, 7.600e+01,
2.100e+01, 1.400e+01, 1.100e+01, 9.000e+00, 5.000e+00, 2.000e+00,
2.000e+00, 0.000e+00, nan, nan, nan, nan,
nan, nan])}],
'layout': {'legend': {'tracegroupgap': 0}, 'template': '...', 'title': {'text': 'Pinot Meunier'}}
})]
gr_list[0].show()
app4 = JupyterDash(__name__)
app4.layout = html.Div([
html.P("Grape:"),
dcc.Dropdown(
id='this_drop',
value='Cabernet Sauvignon',
options=[{'value': x, 'label': x}
for x in red_types],
clearable=False
),
# html.P("Values:"),
# dcc.Dropdown(
# id='values',
# value='total_bill',
# options=[{'value': x, 'label': x}
# for x in ['total_bill', 'tip', 'size']],
# clearable=False
# ),
dcc.Graph(id="pie-chart")
])
@app4.callback(
Output("pie-chart", "fig"),
[Input("this_drop", "value")])
def generate_chart(value):
red2 = red # copy of df??
print(value)
def switch(argument):
switcher = {
'Cabernet Sauvignon': 0,
'Merlot': 1,
'Tempranillo': 2,
'Syrah': 3,
'Garnacha Tinta': 4,
'Pinot Noir': 5,
'Sangiovese': 6,
'Bobal': 7,
'Cabernet Franc': 8,
'Côt': 9,
'Monastrell': 10,
'Mazuelo': 11,
'Alicante Henri Bouschet': 12,
'Tribidrag': 13,
'Montepulciano': 14,
'Gamay Noir': 15,
'Cinsaut': 16,
'Carmenère': 17,
'Douce Noire': 18,
'Barbera': 19,
'Isabella': 20,
'Blaufränkisch': 21,
'Criolla Grande': 22,
'Pinot Meunier': 23
}
return switcher.get(argument)
fig = gr_list[switch(value)].show() # switcher.get(value)
# gr_list[0].show()
return fig
app4.run_server(debug=True, mode='inline')
Cabernet Sauvignon
--------------------------------------------------------------------------- KeyError Traceback (most recent call last) ~/opt/miniconda3/envs/dv_env_geo/lib/python3.9/site-packages/dash/dash.py in dispatch( self=<jupyter_dash.jupyter_app.JupyterDash object> ) 1074 try: -> 1075 func = self.callback_map[output]["callback"] func = undefined self.callback_map = {'pie-chart.fig': {'inputs': [{'id': 'this_drop', 'property': 'value'}], 'state': [], 'callback': <function generate_chart at 0x15e697790>}} output = 'choropleth.figure' 1076 except KeyError: KeyError: 'choropleth.figure' During handling of the above exception, another exception occurred: KeyError Traceback (most recent call last) ~/opt/miniconda3/envs/dv_env_geo/lib/python3.9/site-packages/flask/app.py in full_dispatch_request(self=<Flask '__main__'>) 1511 rv = self.preprocess_request() 1512 if rv is None: -> 1513 rv = self.dispatch_request() rv = None self.dispatch_request = <bound method Flask.dispatch_request of <Flask '__main__'>> 1514 except Exception as e: 1515 rv = self.handle_user_exception(e) ~/opt/miniconda3/envs/dv_env_geo/lib/python3.9/site-packages/flask/app.py in dispatch_request(self=<Flask '__main__'>) 1497 return self.make_default_options_response() 1498 # otherwise dispatch to the handler for that endpoint -> 1499 return self.ensure_sync(self.view_functions[rule.endpoint])(**req.view_args) self.ensure_sync = <bound method Flask.ensure_sync of <Flask '__main__'>> self.view_functions = {'static': <function Flask.__init__.<locals>.<lambda> at 0x15e4a2af0>, '_dash_assets.static': <bound method Scaffold.send_static_file of <Blueprint '_dash_assets'>>, '/_dash-component-suites/<string:package_name>/<path:fingerprinted_path>': <bound method Dash.serve_component_suites of <jupyter_dash.jupyter_app.JupyterDash object at 0x15e6b9eb0>>, '/_dash-layout': <bound method Dash.serve_layout of <jupyter_dash.jupyter_app.JupyterDash object at 0x15e6b9eb0>>, '/_dash-dependencies': <bound method Dash.dependencies of <jupyter_dash.jupyter_app.JupyterDash object at 0x15e6b9eb0>>, '/_dash-update-component': <bound method Dash.dispatch of <jupyter_dash.jupyter_app.JupyterDash object at 0x15e6b9eb0>>, '/_reload-hash': <bound method Dash.serve_reload_hash of <jupyter_dash.jupyter_app.JupyterDash object at 0x15e6b9eb0>>, '/_favicon.ico': <function Dash._serve_default_favicon at 0x15db51280>, '/': <bound method Dash.index of <jupyter_dash.jupyter_app.JupyterDash object at 0x15e6b9eb0>>, '/<path:path>': <bound method Dash.index of <jupyter_dash.jupyter_app.JupyterDash object at 0x15e6b9eb0>>, 'shutdown': <function JupyterDash.__init__.<locals>.shutdown at 0x15e6430d0>, 'alive': <function JupyterDash.__init__.<locals>.alive at 0x15e6431f0>} rule.endpoint = '/_dash-update-component' req.view_args = {} 1500 1501 def full_dispatch_request(self) -> Response: ~/opt/miniconda3/envs/dv_env_geo/lib/python3.9/site-packages/dash/dash.py in dispatch( self=<jupyter_dash.jupyter_app.JupyterDash object> ) 1076 except KeyError: 1077 msg = "Callback function not found for output '{}', perhaps you forgot to prepend the '@'?" -> 1078 raise KeyError(msg.format(output)) global KeyError = undefined msg.format = <built-in method format of str object at 0x15d1c7ae0> output = 'choropleth.figure' 1079 response.set_data(func(*args, outputs_list=outputs_list)) 1080 return response KeyError: "Callback function not found for output 'choropleth.figure', perhaps you forgot to prepend the '@'?"
In the above, the selection works, but it ADDS the next selection to the display, rather than overwriting the previous.
THANKS!!